// 格式化数字（保留小数位）
export const formatNumber = (value, decimals = 2) => {
  if (value === null || value === undefined) return '-';
  return Number(value).toFixed(decimals);
};

// 格式化日期时间
export const formatDateTime = (dateStr) => {
  if (!dateStr) return '';
  const date = new Date(dateStr.replace('T', ' ').replace(/\.\d+$/, ''));
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};

// 格式化日期
export const formatDate = (dateStr) => {
  if (!dateStr) return '';
  const date = new Date(dateStr.replace('T', ' ').replace(/\.\d+$/, ''));
  return date.toLocaleDateString('zh-CN');
};

// 格式化距离（米转公里）
export const formatDistance = (meters) => {
  if (!meters) return '0.00';
  return (meters / 1000).toFixed(2);
};

// 格式化持续时间
export const formatDuration = (seconds) => {
  if (!seconds) return '00:00';
  const hrs = Math.floor(seconds / 3600);
  const mins = Math.floor((seconds % 3600) / 60);
  const secs = Math.floor(seconds % 60);
  return `${hrs > 0 ? hrs + ':' : ''}${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
};

// 格式化配速
// 修正配速计算逻辑，接收速度(m/s)作为参数
export function formatPace(speed) {
  if (!speed || speed === 0 || isNaN(speed)) return '0:00';
  
  // 从速度(m/s)计算每公里秒数
  const paceSecondsPerKm = 1000 / speed;
  let minutes = Math.floor(paceSecondsPerKm / 60);
  let seconds = Math.round(paceSecondsPerKm % 60);
  
  // 处理秒数进位问题
  if (seconds === 60) {
    minutes += 1;
    seconds = 0;
  }
  
  return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}

// 计算百分比
export const calculatePercentage = (value, min, max) => {
  return Math.max(0, Math.min(100, ((value - min) / (max - min)) * 100));
};

// 获取分段类型
export const getSplitType = (type) => {
  const types = {
    'INTERVAL_ACTIVE': '活动',
    'RWD_STAND': '站立',
    'RWD_RUN': '跑步',
    'RWD_WALK': '步行'
  };
  return types[type] || type;
};

// 获取分段徽章类
export const getSplitBadgeClass = (type) => {
  const classes = {
    'INTERVAL_ACTIVE': 'bg-blue-100 text-blue-800',
    'RWD_STAND': 'bg-gray-100 text-gray-800',
    'RWD_RUN': 'bg-green-100 text-green-800',
    'RWD_WALK': 'bg-yellow-100 text-yellow-800'
  };
  return classes[type] || 'bg-gray-100 text-gray-800';
};

// 触地时间反馈
export const getContactTimeFeedback = (time) => {
  if (time < 220) return '优秀 (少于220ms)';
  if (time < 250) return '良好 (220-250ms)';
  return '待改进 (大于250ms)';
};

// 垂直振幅反馈
export const getOscillationFeedback = (osc) => {
  if (osc < 9) return '优秀 (少于9cm)';
  if (osc < 11) return '良好 (9-11cm)';
  return '待改进 (大于11cm)';
};

// 训练效果描述
export const getTrainingEffectDescription = (effect) => {
  if (effect < 1) return '恢复训练';
  if (effect < 2) return '轻度训练';
  if (effect < 3) return '维持训练';
  if (effect < 4) return '提升训练';
  if (effect < 5) return '高强度训练';
  return '极限训练';
};

// 添加新的时间格式化函数
export function formatDurationShort(totalSeconds) {
  if (!totalSeconds || isNaN(totalSeconds)) return '0:00';
  
  let minutes = Math.floor(totalSeconds / 60);
  let seconds = Math.round(totalSeconds % 60);
  
  // 处理秒数进位问题
  if (seconds === 60) {
    minutes += 1;
    seconds = 0;
  }
  
  return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}